<?php

/**
 * 信息查询
 *
 * @author suning <snsnsky@gmail.com>
 */
use \Common\Bridge;

define('SITE_AUTH', 'admin-smslog');
require('../../../common.inc.php');

class smslog extends \lib\core\AdminAction
{
    /*
     * 定义短信表名
     */
    const TABLE = 'SmsLog';

    public function doDefault()
    {
        $smarty = $this->app->page();

        $mobile = $_GET['Mobile'];
        $type = $_GET['Type'];
        $start_time = trim($_GET['start_time']);
        $end_time = trim($_GET['end_time']);
        $page = max(1, intval($_GET['page']));

        $where = [];
        if (!empty($mobile))
            $where['Mobile'] = $mobile;
        if($type!='')
            $where['Type'] = $type;
        if (!empty($start_time))
            $where['CreateTime >='] = $start_time;
        if (!empty($end_time))
            $where['CreateTime <='] = $end_time;

        $pdo_r = Bridge::pdo(Bridge::DB_ADMIN_R);
        $total = $pdo_r->clear()
            ->select('count(SmsId)')
            ->from(self::TABLE)
            ->where($where)
            ->getvalue();

        if ($total > 0)
        {
            $data = $pdo_r->clear()
                ->select('*')
                ->from(self::TABLE)
                ->where($where)
                ->order('SmsId DESC')
                ->limit(($page - 1) * self::PAGE_SIZE, self::PAGE_SIZE)
                ->getAll();

            $smarty->value('data', $data);

            $pagination = $this->pagination($total, self::PAGE_SIZE, $page);
            $smarty->value('pagination', $pagination);
        }

        $smarty->value('Mobile', $mobile);
        $smarty->value('Type', $type);
        $smarty->value('start_time', $start_time);
        $smarty->value('end_time', $end_time);
        $smarty->value('total', $total);
        $smarty->value('total', $total);
        $smarty->value('total', $total);
        $smarty->output('admin/smslog.tpl');
    }

    public function doRemainMsg()
    {
        $sms = \Notice\Sms::getBalanceCount();
        $this->output(['count' => (int) $sms]);
    }
}
$app->run();